<?php
//  防止全局变量造成安全隐患
header('Content-Type:application/json');  //此声明非常重要
include('connect.php');
$admin = false;
//  启动会话，这步必不可少
session_start();
//  判断是否登陆
if (isset($_SESSION["admin"]) && $_SESSION["admin"] === true) {
	$sql="select access_token from token";
		$notice =$db->query($sql);
		if ($notice->num_rows > 0) {
			
		}else{
			$url_gettoken='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx075c9c546d022107&secret=2a488834a0b1b3b4450568aec536e85f';
			$token=file_get_contents($url_gettoken);//发送HTTPs请求并获取返回的数据，推荐使用curl
			$json = json_decode($token);//对json数据解码
			$arr = get_object_vars($json);
			$access_token=$arr['access_token'];//token
			$expires_in=$arr['expires_in'];//过期时间
			$time=time();
			echo $access_token;
			$sql="INSERT INTO access_token (access_token,expires_in,in_time) VALUES ('{$access_token}', '{$expires_in}','{$time}' )";
			$addtoken =$db->query($sql);
		}
   $json['code']=1;
    echo (json_encode($json)) ;
} else {
    //  验证失败，将 $_SESSION["admin"] 置为 false
    $_SESSION["admin"] = false;
    $json['code']=0;
     echo (json_encode($json)) ;
}
?>